﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Markup;
using System.Reflection;
using System.IO;

namespace Goldlight.Mvvm.Base.Infrastructure.Skins
{
    public static class BamlHelper
    {
        static BamlHelper()
        {
            Type type = typeof(XamlReader);
            // Hope that Microsoft will not change this in the future, or at least provide an official way to load baml
            LoadBamlMethod = type.GetMethod("LoadBaml", BindingFlags.NonPublic | BindingFlags.Static);
        }

        public static TRoot LoadBaml<TRoot>(Stream stream)
        {
            object[] parameters = new object[] { stream, new ParserContext(), null, false };
            object bamlRoot = LoadBamlMethod.Invoke(null, parameters);
            return (TRoot)bamlRoot;
        }

        private static readonly MethodInfo LoadBamlMethod;
    }
}
